﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DataAccessLayer;
using Entities;
using BusinessLogicLayer;
using BusinessLogicLayer.Validation;

namespace QuanLyTKB_TienDoGD.QuanLyTKB
{
    public partial class frmThemTKB : Office2007Form
    {
        IRepository<ThoiKhoaBieu> _repository;
        ThoiKhoaBieuManager _manager;
        ThoiKhoaBieu tkb;
        frmThoiKhoaBieu frmCha;
        bool _isUpdate;


        public frmThemTKB()
        {
            InitializeComponent();
        }


        public frmThemTKB(ThoiKhoaBieuManager mana, frmThoiKhoaBieu frm, ThoiKhoaBieu _tkb = null)
            : this()
        {
            _manager = mana;
            tkb = _tkb;
            _isUpdate = (_tkb != null);
            frmCha = frm;
        }
        
        private void frmThemTKB_Load(object sender, EventArgs e)
        {
            LoadAllCBB();
            if (_isUpdate)
            {
                txtMaTKB.ReadOnly = true;
                txtMaTKB.Text = tkb.MaTKB;
                cbbLopHoc.SelectedValue = tkb.MaLop;
                cbbMonHoc.SelectedValue = tkb.MaMH;
                cbbGiaoVien.SelectedValue = tkb.MaGV;
                nrdTuan.Text = tkb.Tuan.ToString();
                dtpBatDau.Value = tkb.NgayBatDau;                
                cbbThu.Text = tkb.Thu;
                cbbTiet.Text = tkb.Tiet;
                txtPhong.Text = tkb.Phong;
            }
            _repository = new Repository<ThoiKhoaBieu>();
            _manager = new ThoiKhoaBieuManager(_repository);   
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tkb = new ThoiKhoaBieu();

            tkb.MaTKB = txtMaTKB.Text;
            
            tkb.MaLop = cbbLopHoc.SelectedValue.ToString();
           
            tkb.MaMH = cbbMonHoc.SelectedValue.ToString();
           
            tkb.MaGV = cbbGiaoVien.SelectedValue.ToString();
            tkb.Tuan = Convert.ToInt32(nrdTuan.Text);
            tkb.NgayBatDau = dtpBatDau.Value;             
            tkb.NgayKetThuc = dtpBatDau.Value.AddDays(6);
            tkb.Thu = cbbThu.Text;
            tkb.Tiet = cbbTiet.Text;
            tkb.Phong = txtPhong.Text;

            var vResult = _manager.Validate(tkb);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(tkb);
                if (aResult.Success)
                {
                    frmCha.btnLamMoi.PerformClick();
                    if (_isUpdate)
                    {
                        MessageBox.Show("Cập nhật thời khóa biểu thành công");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Thêm thời khóa biểu thành công");
                        btnMacDinh.PerformClick();
                    }
                }
                else
                    MessageBox.Show(aResult.GetErrosMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }


        private void btnMacDinh_Click(object sender, EventArgs e)
        {
            txtMaTKB.ResetText();
            cbbLopHoc.ResetText();
            cbbMonHoc.ResetText();
            cbbGiaoVien.ResetText();
            nrdTuan.ResetText();
            dtpBatDau.ResetText();
            cbbThu.ResetText();
            cbbTiet.ResetText();
            txtPhong.ResetText();
        }

        private void LoadCBBLopHoc()
        {
            cbbLopHoc.SelectedValue = "0";
            List<LopHoc> ds = LopHoc.Repository.GetAll().ToList();
            LopHoc lh = new LopHoc();
            lh.MaLop = "------- All ------";
            ds.Insert(0, lh);
            cbbLopHoc.DataSource = ds;
            cbbLopHoc.DisplayMember = "MaLop";
            cbbLopHoc.ValueMember = "MaLop";
            cbbLopHoc.SelectedIndex = 0;
        }

        private void LoadCbbMonHoc()
        {
            cbbMonHoc.SelectedValue = "0";
            List<MonHoc> ds = MonHoc.Repository.GetAll().ToList();
            MonHoc mh = new MonHoc();
            mh.TenMH = "------- All ------";
            mh.MaMH = "";
            ds.Insert(0, mh);
            cbbMonHoc.DataSource = ds;
            cbbMonHoc.DisplayMember = "TenMH";
            cbbMonHoc.ValueMember = "MaMH";
            cbbMonHoc.SelectedIndex = 0;
        }

        public void LoadCbbGiaoVien()
        {
            cbbGiaoVien.SelectedValue = "0";
            List<GiaoVien> ds = GiaoVien.Repository.GetAll().ToList();
            GiaoVien gv = new GiaoVien();
            gv.TenGV = "------- All ------";
            gv.MaGV = "";
            ds.Insert(0, gv);
            cbbGiaoVien.DataSource = ds;
            cbbGiaoVien.DisplayMember = "TenGV";
            cbbGiaoVien.ValueMember = "MaGV";
            cbbGiaoVien.SelectedIndex = 0;
        }
        public void LoadAllCBB()
        {
            LoadCBBLopHoc();
            LoadCbbMonHoc();
            LoadCbbGiaoVien();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtpBatDau_TextChanged(object sender, EventArgs e)
        {
            DateTime BatDau = dtpBatDau.Value;
            if (BatDau.DayOfWeek != DayOfWeek.Monday)
            {
                MessageBox.Show("Ngày đầu phải là thứ 2 hàng tuần", "Chọn ngày không hợp lệ");
                dtpBatDau.ResetText();
            }
        }

    }
}
